package org.windowkit.toolkit.icons;

import java.awt.Component;
import java.awt.Graphics2D;
import java.awt.Rectangle;

/**
 * @author Eric Lundin
 *
 */
public class ArrowLineIcon extends RotatableIcon {
	/**
	 * The percentage of total icon width which will be the rectangle's width.
	 */
	private static final double RECTANGLE_WIDTH_PCT = .4d;

	/**
	 * The percentage of the total icon width which will be the gap between the
	 * rectangle and the line.
	 */
	private static final double GAP_PCT = .2d;

	/**
	 * @param direction
	 *            The direction the arrow should face. Must be one of:
	 *            <ul>
	 *            <li>SwingConstants.NORTH</li>
	 *            <li>SwingConstants.SOUTH</li>
	 *            <li>SwingConstants.WEST</li>
	 *            <li>SwingConstants.EAST</li>
	 *            </ul>
	 */
	public ArrowLineIcon(final int direction) {
		super(direction);

	}

	/**
	 * Get the y values for the points making up the arrow.
	 *
	 * @param height
	 *            The component height.
	 * @param width
	 *            The component width.
	 * @return The y values for the arrow's points.
	 */
	public int[] getYPoints() {
		int[] yPoints = new int[3];
		yPoints[0] = getYLimit();
		yPoints[1] = -getYLimit();
		yPoints[2] = 0;
		return yPoints;
	}

	/**
	 * Get the x values for the points making up the arrow.
	 *
	 * @param height
	 *            The component height.
	 * @param width
	 *            The component width.
	 * @return The x values for the arrow's points.
	 */
	public int[] getXPoints() {
		int[] xPoints = new int[3];
		xPoints[0] = -(int) (getXLimit() * (1 - (RECTANGLE_WIDTH_PCT + GAP_PCT)));
		xPoints[1] = -(int) (getXLimit() * (1 - (RECTANGLE_WIDTH_PCT + GAP_PCT)));
		xPoints[2] = getXLimit();
		return xPoints;
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see org.windowkit.toolkit.icons.RotatableIcon#drawIcon(java.awt.Component,
	 *      java.awt.Graphics2D, int, int)
	 */
	@Override
	public void drawIcon(final Component c, final Graphics2D g2) {
		g2.setColor(c.getForeground());
		g2.fillPolygon(getXPoints(), getYPoints(), 3);
		g2.fill(createRectangle());

	}

	/**
	 * Create the line part of this icon.
	 *
	 * @return The correctly sized rectangle.
	 */
	public Rectangle createRectangle() {
		int x, y, width, height;
		x = -getXLimit();
		y = -getYLimit();
		width = (int) (getXLimit() * RECTANGLE_WIDTH_PCT);
		height = getYLimit() * 2;
		return new Rectangle(x, y, width, height);
	}

}
